package com.ssy.jt808.gateway.domain.converter;

import com.ssy.jt808.gateway.infrastructure.constant.Jt808Constant;
import com.ssy.jt808.gateway.sdk.terminal.constant.location.addition.AdasAlarmEnum;
import com.ssy.jt808.gateway.sdk.terminal.constant.location.addition.AdasAlarmExtraFieldEnum;
import com.ssy.jt808.gateway.sdk.terminal.pojo.info.alarm.AdasAlarmInfo;
import com.ssy.jt808.gateway.sdk.terminal.pojo.info.alarm.AlarmIdentificationInfo;
import com.ssy.jt808.protocol.commons.transform.attribute.AlarmADAS;

import java.util.HashMap;
import java.util.Map;

import static com.ssy.jt808.gateway.sdk.terminal.constant.location.addition.AdasAlarmExtraFieldEnum.*;

/**
 * @author wangzezhou
 */
public class AdasAlarmInfoConverter{

  public static AdasAlarmInfo convertAdasAlarmInfo( AlarmADAS alarmADAS ){

    AdasAlarmEnum type = AdasAlarmEnum.type(alarmADAS.getType());
    AdasAlarmInfo adasAlarmInfo = new AdasAlarmInfo();
    adasAlarmInfo.setId(alarmADAS.getId());
    adasAlarmInfo.setState(alarmADAS.getState());
    adasAlarmInfo.setType(type);
    adasAlarmInfo.setLevel(alarmADAS.getLevel());
    adasAlarmInfo.setAltitude(alarmADAS.getAltitude());
    adasAlarmInfo.setLat((float)(alarmADAS.getLatitude()/Jt808Constant.ONE_MILLION));
    adasAlarmInfo.setLng((float)(alarmADAS.getLongitude()/Jt808Constant.ONE_MILLION));
    adasAlarmInfo.setAlarmTime(alarmADAS.getAlarmTime());
    adasAlarmInfo.setVehicleStateBits(alarmADAS.getStatusBit());

    Map<AdasAlarmExtraFieldEnum,Object> extraInfo = convertExtraInfo(type, alarmADAS);
    adasAlarmInfo.setExtraInfo(extraInfo);

    // 报警标识
    AlarmIdentificationInfo alarmIdentificationInfo = new AlarmIdentificationInfo();
    adasAlarmInfo.setAlarmIdentification(alarmIdentificationInfo);
    alarmIdentificationInfo.setDeviceId(alarmADAS.getDeviceId());
    alarmIdentificationInfo.setTime(alarmADAS.getDateTime());
    alarmIdentificationInfo.setSequenceNo(alarmADAS.getSequenceNo());
    alarmIdentificationInfo.setAttachmentNum(alarmADAS.getAttachmentNum());
    return adasAlarmInfo;
  }

  private static Map<AdasAlarmExtraFieldEnum,Object> convertExtraInfo( AdasAlarmEnum type, AlarmADAS alarm ){
    Map<AdasAlarmExtraFieldEnum,Object> extraInfo = new HashMap<>(2);
    switch(type){
      case ForwardCollisionWarning:{
        extraInfo.put(FrontCarSpeed, alarm.getFrontSpeed());
        extraInfo.put(FrontObjectDistance, alarm.getFrontDistance());
        break;
      }
      case LaneDeparture:{
        extraInfo.put(FrontCarSpeed, alarm.getFrontSpeed());
        extraInfo.put(FrontObjectDistance, alarm.getFrontDistance());
        extraInfo.put(DeviateType, alarm.getDeviateType());
        break;
      }
      case PedestrianCollision:{
        extraInfo.put(FrontObjectDistance,alarm.getFrontDistance());
        break;
      }
      case RoadSignRecognitionEvent:
      case RoadSignOverLimit:{
        extraInfo.put(RoadSign,alarm.getRoadSign());
        break;
      }
      default:
        break;
    }
    return extraInfo;
  }

}
